package com.dream.launcher;

import com.dream.zhujianjun.R;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScreenIndicator extends LinearLayout
{
	private LinearLayout mIndicatorLayout;
	private int mPrevIndex = 0;
	private int mCurrIndex = 0;
	private Context mContext;
	private int mCount = 0;
	
	private Animation mAnimBig;
	private Animation mAnimSmall;
	
	static final String TAG = "ScreenIndicator";
	
	public ScreenIndicator(Context context) 
	{
		this(context, null);
	}

	public ScreenIndicator(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		mContext = context;
		initViews();
	}
	
	public void updateIndicator(int count, int current)
	{
		if(mCurrIndex == current) 
		{
			Log.v(TAG, "updateIndicator: update current index " + current + ", ignore (mCurrIndex == current)");
			return;
		}
		
		if(mPrevIndex == current)
		{
			Log.v(TAG, "updateIndicator: update current index " + current + ", ignore (mPrevIndex == current)");
			return;
		}
		
		mCurrIndex = current;
		Log.v(TAG, "updateIndicator: Prev = " + mPrevIndex + ", Current = " + String.valueOf(mCurrIndex));
		
		FrameLayout l = (FrameLayout) mIndicatorLayout.getChildAt(mPrevIndex - 1);
		ImageView p = (ImageView) l.getChildAt(0);
		TextView  t = (TextView) l.getChildAt(1);
		ImageView c = (ImageView) mIndicatorLayout.getChildAt(mCurrIndex - 1);
		p.startAnimation(mAnimSmall);
		t.setText("");
		c.startAnimation(mAnimBig);
		
		mAnimBig.setAnimationListener(animListener);
	}

	public void update(int count, int current) 
	{
		if( count <= 0 || current <= 0 )
			return;
		mCount = count;
		
		mIndicatorLayout.invalidate();
		mIndicatorLayout.removeAllViews();
		
		for(int i = 0; i < count; i++)
		{
			LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			p.leftMargin = 10;
			p.rightMargin = 10;
			p.topMargin = 10;
			p.bottomMargin = 10;
			
			FrameLayout currentLayout = new FrameLayout(mContext);
			ImageView v = new ImageView(mContext);
			if(current - 1 == i)
			{
				v.setBackgroundResource(R.drawable.indicator_big);
				
				TextView textView = new TextView(mContext);
				textView.setText(String.valueOf(current));
				textView.setTextSize(14);
				textView.setTextColor(Color.BLACK);
				
				FrameLayout.LayoutParams pam = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
				pam.gravity = Gravity.CENTER;
				
				currentLayout.addView(v);
				currentLayout.addView(textView, pam);
				
				mIndicatorLayout.addView(currentLayout, p);
			}
			else
			{
				v.setBackgroundResource(R.drawable.indicator_small);
				v.setTag(Integer.valueOf(i));
				v.setOnClickListener(onClicked);
				
				mIndicatorLayout.addView(v, p);
			}
		}
		
		mPrevIndex = current;
	}
	
	private void initViews() 
	{
		LayoutInflater.from(getContext()).inflate(R.layout.indicator, this);
		mIndicatorLayout = (LinearLayout) findViewById(R.id.indicator);
		
		mAnimBig = AnimationUtils.loadAnimation(mContext, R.anim.scalebig);
		mAnimSmall = AnimationUtils.loadAnimation(mContext, R.anim.scalesmall);
	}
	
	View.OnClickListener onClicked = new View.OnClickListener()
	{
		public void onClick(View v) 
		{
			Integer i = (Integer) v.getTag();
			int pos = i.intValue();
			mCurrIndex = pos + 1;
			
			// Prev -> Scale small, Current - Scale big
			Log.v(TAG, "onClick: Prev = " + mPrevIndex + ", Current = " + String.valueOf(mCurrIndex));
			FrameLayout l = (FrameLayout) mIndicatorLayout.getChildAt(mPrevIndex - 1);
			ImageView p = (ImageView) l.getChildAt(0);
			TextView  t = (TextView) l.getChildAt(1);
			ImageView c = (ImageView) mIndicatorLayout.getChildAt(mCurrIndex - 1);
			p.startAnimation(mAnimSmall);
			t.setText("");
			c.startAnimation(mAnimBig);
			
			mAnimBig.setAnimationListener(animListener);
		}
	};
	
	Animation.AnimationListener animListener = new Animation.AnimationListener()
	{
		public void onAnimationStart(Animation animation) 
		{
			// do nothing
		}
		
		public void onAnimationRepeat(Animation animation) 
		{
			// do nothing
		}
		
		public void onAnimationEnd(Animation animation)
		{
			update(mCount, mCurrIndex);
		}
	};
	
	public int getCurrentIndex()
	{
		return mCurrIndex;
	}
	
	public int getTotalPage()
	{
		return mCount;
	}
	
	public void resetCurrentIndex()
	{
		mCurrIndex = 0;
	}
	
	public void resetTotalPage()
	{
		mCount = 0;
	}
	
//	public static interface ScreenIndicatorClickListener
//	{
//		public void onIndicatorClicked(int current);
//	}
}